package soccer.stats

class PlayerHistoryController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [playerHistoryInstanceList: PlayerHistory.list(params), playerHistoryInstanceTotal: PlayerHistory.count()]
    }

    def create = {
        def playerHistoryInstance = new PlayerHistory()
        playerHistoryInstance.properties = params
        return [playerHistoryInstance: playerHistoryInstance]
    }

    def save = {
        def playerHistoryInstance = new PlayerHistory(params)
        if (playerHistoryInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'playerHistory.label', default: 'PlayerHistory'), playerHistoryInstance.id])}"
            redirect(action: "show", id: playerHistoryInstance.id)
        }
        else {
            render(view: "create", model: [playerHistoryInstance: playerHistoryInstance])
        }
    }

    def show = {
        def playerHistoryInstance = PlayerHistory.get(params.id)
        if (!playerHistoryInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'playerHistory.label', default: 'PlayerHistory'), params.id])}"
            redirect(action: "list")
        }
        else {
            [playerHistoryInstance: playerHistoryInstance]
        }
    }

    def edit = {
        def playerHistoryInstance = PlayerHistory.get(params.id)
        if (!playerHistoryInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'playerHistory.label', default: 'PlayerHistory'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [playerHistoryInstance: playerHistoryInstance]
        }
    }

    def update = {
        def playerHistoryInstance = PlayerHistory.get(params.id)
        if (playerHistoryInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (playerHistoryInstance.version > version) {

                    playerHistoryInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'playerHistory.label', default: 'PlayerHistory')] as Object[], "Another user has updated this PlayerHistory while you were editing")
                    render(view: "edit", model: [playerHistoryInstance: playerHistoryInstance])
                    return
                }
            }
            playerHistoryInstance.properties = params
            if (!playerHistoryInstance.hasErrors() && playerHistoryInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'playerHistory.label', default: 'PlayerHistory'), playerHistoryInstance.id])}"
                redirect(action: "show", id: playerHistoryInstance.id)
            }
            else {
                render(view: "edit", model: [playerHistoryInstance: playerHistoryInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'playerHistory.label', default: 'PlayerHistory'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def playerHistoryInstance = PlayerHistory.get(params.id)
        if (playerHistoryInstance) {
            try {
                playerHistoryInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'playerHistory.label', default: 'PlayerHistory'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'playerHistory.label', default: 'PlayerHistory'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'playerHistory.label', default: 'PlayerHistory'), params.id])}"
            redirect(action: "list")
        }
    }
}
